// pages/product-home/product-home.ts
import { PAGE_SEARCH_RESULTS, PAGE_PRODUCT_LIST_PROCESSING, PAGE_PRODUCT_LIST_MATERIAL, PAGE_CONSULTATION, goToPage } from '../../../config/pages.js'

Page({
  data: {
    // 当前选中的分类
    currentCategory: 'single-tape' as string,
    
    // 材料分类数据
    materialData: {
      'single-tape': {
        name: '单面胶带',
        materials: [
          '美纹纸胶带',
          '醋酸布胶带',
          '玻璃纤维胶带',
          'PI胶带',
          'PET胶带',
          'PP/PE胶带',
          '特氟龙胶带',
          '铜箔胶带',
          '铝箔胶带',
          'PVC胶带'
        ]
      },
      'double-tape': {
        name: '双面胶带',
        materials: [
          '无基材双面胶',
          'PET双面胶',
          '棉纸双面胶',
          '无纺布双面胶',
          '泡棉双面胶',
          '纯胶膜',
          '强粘胶带',
          'AB胶',
          '纳米胶',
          '易撕胶带'
        ]
      },
      'protective-film': {
        name: '保护膜',
        materials: [
          'PI保护膜',
          '硅胶保护膜',
          'PET保护膜',
          'CPP保护膜',
          'PE保护膜',
          'PP保护膜',
          'UV减粘膜'
        ]
      },
      'magnetic-material': {
        name: '电磁屏蔽材料',
        materials: [
          '异向导电膜',
          '单向导电膜',
          '导电泡棉',
          '双向导电带'
        ]
      },
      'absorption-material': {
        name: '吸波材料',
        materials: [
          '铁氧体',
          '导磁片',
          'OCA',
          '遮光胶带'
        ]
      },
      'optical-material': {
        name: '光学材料',
        materials: [
          '偏光片',
          '反射片',
          '扩散片',
          '增亮膜',
          '背光膜',
          '其它',
          '纱网'
        ]
      },
      'acoustic-material': {
        name: '声学材料',
        materials: [
          '防水透气膜',
          '防水透声膜'
        ]
      },
      'label-material': {
        name: '标签材料',
        materials: [
          '压敏标签',
          '热敏标签',
          '防伪标签',
          '耐高温标签',
          '遇水消失标签'
        ]
      },
      'thermal-material': {
        name: '热界面材料',
        materials: [
          '导热垫片',
          '导热石墨',
          '导热泡棉',
          '石墨烯',
          '导热胶带'
        ]
      },
      'thin-film': {
        name: '薄膜',
        materials: [
          'PI',
          'PET',
          'PC',
          '铝箔',
          '铜箔'
        ]
      },
      'foam': {
        name: '泡棉',
        materials: [
          'PU泡棉',
          'PE泡棉',
          'EVA泡棉',
          'EPDM泡棉',
          'CR泡棉',
          '硅胶胶',
          '气凝胶',
          '其它'
        ]
      },
      'die-cut': {
        name: '模切辅料',
        materials: [
          '离型纸',
          '离型膜',
          '菲林膜',
          '转移膜',
          '载体膜'
        ]
      },
      'leather-tape': {
        name: '皮胶带',
        materials: [
          '舒力克（3M）',
          '国产'
        ]
      },
      'insulation-paper': {
        name: '绝缘纸',
        materials: [
          '进口',
          '国产'
        ]
      }
    }
  },

  onLoad(options: any) {
    console.log('产品分类页面加载完成');
    
    // 检查URL参数，确定初始分类
    if (options.category) {
      this.setData({
        currentCategory: options.category
      });
    }
  },

  onShow() {
    console.log('产品分类页面显示');
  },

  // 返回上一页
  goBack() {
    wx.navigateBack({
      delta: 1
    });
  },

  // 打开搜索
  openSearch() {
    goToPage(PAGE_SEARCH_RESULTS);
  },

  // 切换分类
  switchCategory(e: any) {
    const category = e.currentTarget.dataset.category;
    if (category === this.data.currentCategory) return;
    
    this.setData({
      currentCategory: category
    });
    
    // 滚动到对应分类区域
    this.scrollToCategory(category);
  },

  // 滚动到指定分类
  scrollToCategory(category: string) {
    const query = wx.createSelectorQuery();
    query.select(`#category-${category}`).boundingClientRect();
    query.select('#materialsContent').boundingClientRect();
    query.exec((res) => {
      if (res[0] && res[1]) {
        const categoryRect = res[0];
        const containerRect = res[1];
        const scrollTop = categoryRect.top - containerRect.top + containerRect.scrollTop;
        
        wx.pageScrollTo({
          scrollTop: scrollTop,
          duration: 300
        });
      }
    });
  },

  // 查看材料详情
  viewMaterial(e: any) {
    const materialName = e.currentTarget.dataset.name;
    console.log('查看材料详情:', materialName);
    
    // 跳转到材料一览页面
    goToPage(PAGE_PRODUCT_LIST_MATERIAL, { category: materialName });
  },

  // 广告点击事件
  goToAd() {
    // 跳转到咨询页面
    goToPage(PAGE_CONSULTATION, { type: 'material' });
  }
});
